<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui">

    <ui:composition template="/template/DefaultTemplate.xhtml">
        <ui:define name="title">
            <h:outputText value="Master Production Schedule"></h:outputText>
        </ui:define>
        <ui:define name="content">
            <h:form prependId="false">
                <p:panel header="Weekly Demand Management for #{masterProductionScheduleWeeklyEntityController.selected.monthOfProduction} #{masterProductionScheduleWeeklyEntityController.selected.yearOfProduction} (Week #{masterProductionScheduleWeeklyEntityController.selected.weekOfProduction})" toggleable="true" toggleSpeed="500" collapsed="false">
                    <h:panelGroup rendered="#{masterProductionScheduleWeeklyEntityController.items.rowCount > 0}">
                        <p:fieldset legend="Overall Sales Operation Plan Information (#{masterProductionScheduleWeeklyEntityController.selected.monthOfProduction})">
                            <h:panelGrid columns="2">
                                <h:outputText value="On Hand Inventory From Previous Month:"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.previousMonthOnHand} cases"/>
                            </h:panelGrid>
                            <p:dataTable value="#{masterProductionScheduleWeeklyEntityController.createItems}" 
                                         var="item" style="table-layout:fixed"
                                         rows="6" >

                                <p:column style="width:16px">  
                                    <p:rowToggler />  
                                </p:column>

                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Month"/>
                                    </f:facet>
                                    <h:outputText value="#{item.monthOfProduction}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Working Days"/>
                                    </f:facet>
                                    <h:outputText value="#{item.monthlyWorkingDays}"/>
                                </p:column>

                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Sales Forecast"/>
                                    </f:facet>
                                    <h:outputText value="#{item.salesForecastFigure}"/>
                                </p:column>

                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Planned Fulfillment"/>
                                    </f:facet>
                                    <h:outputText value="#{item.fulfilledFigure}"/>
                                </p:column>

                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="On Hand"/>
                                    </f:facet>
                                    <h:outputText value="#{item.safetyStock}" rendered="#{masterProductionScheduleMonthlyEntityController.checkNegativeOnHand(item.safetyStock)==false}"/>
                                    <h:outputText value="#{item.safetyStock}" style="color:red" rendered="#{masterProductionScheduleMonthlyEntityController.checkNegativeOnHand(item.safetyStock)==true}"/>
                                </p:column>

                                <p:rowExpansion>  
                                    <h:panelGrid columns="2">
                                        <p:dataTable value="#{item.salesForecast.historicalPPQ}" 
                                                     var="ppq" style="table-layout:fixed"
                                                     rows="6" >

                                            <f:facet name="header">  
                                                <h:outputText value="Sale Forecast Recommendation"/>
                                            </f:facet>
                                            <p:column>
                                                <f:facet name="header">
                                                    <h:outputText value="Product"/>
                                                </f:facet>
                                                <h:outputText value="#{ppq.product.productName}"/>
                                            </p:column>

                                            <p:column>
                                                <f:facet name="header">
                                                    <h:outputText value="Percentage"/>
                                                </f:facet>
                                                <h:outputText value="#{ppq.percentage*100}%"/>
                                            </p:column>

                                        </p:dataTable>
                                        <p:dataTable value="#{item.overallPPQ}" 
                                                     var="ppq" style="table-layout:fixed"
                                                     rows="6" >

                                            <f:facet name="header">  
                                                <h:outputText value="Total Production Allocation"/>
                                            </f:facet>
                                            <p:column>
                                                <f:facet name="header">
                                                    <h:outputText value="Product"/>
                                                </f:facet>
                                                <h:outputText value="#{ppq.product.productName}"/>
                                            </p:column>
                                            <p:column>
                                                <f:facet name="header">
                                                    <h:outputText value="Percentage"/>
                                                </f:facet>
                                                <h:outputText value="#{ppq.percentage*100}%" style="color:green" rendered="#{masterProductionScheduleMonthlyEntityController.checkPercentageEqualsRecommendation(item,ppq)==true}"/>
                                                <h:outputText value="#{ppq.percentage*100}%" style="color:red" rendered="#{masterProductionScheduleMonthlyEntityController.checkPercentageEqualsRecommendation(item,ppq)==false}"/>
                                            </p:column>

                                        </p:dataTable>
                                    </h:panelGrid>
                                </p:rowExpansion>
                            </p:dataTable>
                        </p:fieldset>
                    </h:panelGroup>
                </p:panel>
                <br />
                <p:panel header="Production Line Level: #{materProductionScheduleWeeklyEntityController.selected.productionType} (#{masterProductionScheduleWeeklyEntityController.selected.monthOfProduction} Week #{masterProductionScheduleWeeklyEntityController.selected.weekOfProduction})" toggleable="true" toggleSpeed="500" collapsed="true">
                    <p:tabView>
                        <p:tab title="Master Production Schedule">
                            <p:fieldset legend="Master Production Schedule">
                                <p:dataTable value="#{masterProductionScheduleWeeklyEntityController.createItems}" 
                                             var="item" style="table-layout:fixed"
                                             rows="6" >

                                    <p:columnGroup type="header">  
                                        <p:row> 
                                            <p:column rowspan="2" headerText="Working Days in Month" />
                                            <p:column colspan="2" headerText="Classic Nutri Bar" /> 
                                            <p:column colspan="2" headerText="Premium Power Bar" />
                                        </p:row>   

                                        <p:row>  
                                            <p:column headerText="Nuts" />  
                                            <p:column headerText="Fruits" />  
                                            <p:column headerText="Chocolate Nuts" />  
                                            <p:column headerText="Redcurrant Fruits" />  
                                        </p:row>  
                                    </p:columnGroup>

                                    <p:column>
                                        <h:outputText value="#{item.monthlyWorkingDays}"/>
                                    </p:column>
                                    <p:column>
                                        <h:outputText value="#{item.classicNuts}"/>
                                    </p:column>
                                    <p:column>
                                        <h:outputText value="#{item.classicFruits}"/>
                                    </p:column>
                                    <p:column>
                                        <h:outputText value="#{item.premiumNuts}"/>
                                    </p:column>
                                    <p:column>
                                        <h:outputText value="#{item.premiumFruits}"/>
                                    </p:column>

                                </p:dataTable>
                            </p:fieldset>
                            <br />
                            <p:fieldset legend="Capacity">
                                <p:dataTable value="#{masterProductionScheduleWeeklyEntityController.createItems}" 
                                             var="item" style="table-layout:fixed"
                                             rows="6" >

                                    <p:columnGroup type="header">  
                                        <p:row> 
                                            <p:column rowspan="2" headerText="Production Plan" />
                                            <p:column colspan="2" headerText="Capacity (Hours)" /> 
                                            <p:column rowspan="2" headerText="Change Over Time" />
                                            <p:column colspan="2" headerText="Utilization" />
                                        </p:row>   

                                        <p:row>  
                                            <p:column headerText="Normal" />  
                                            <p:column headerText="Over Time" />  
                                            <p:column headerText="Normal" />  
                                            <p:column headerText="Over Time" />   
                                        </p:row>  
                                    </p:columnGroup>

                                    <p:column>
                                        <h:outputText value="#{item.plannedQuantity}"/>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Capacity (Hours)"/>
                                        </f:facet>
                                        <h:outputText value="#{item.capacityNormal}"/>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Capacity (Over Time)"/>
                                        </f:facet>
                                        <h:outputText value="#{item.capacityOverTime}"/>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Change Over Time (Hours)"/>
                                        </f:facet>
                                        <h:outputText value="#{masterProductionScheduleMonthlyEntityController.calculateMonthlyChangeOverTime(item)}"/>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Utilization"/>
                                        </f:facet>
                                        <h:outputText value="#{item.utilizationNormal}%"/>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Utilization (Over Time)"/>
                                        </f:facet>
                                        <h:outputText value="#{item.utilizationOverTime}%" rendered="#{masterProductionScheduleMonthlyEntityController.checkPercentage(item.utilizationOverTime)==false}"/>
                                        <h:outputText value="#{item.utilizationOverTime}%" style="color:red" rendered="#{masterProductionScheduleMonthlyEntityController.checkPercentage(item.utilizationOverTime)==true}"/>
                                    </p:column>

                                </p:dataTable>
                            </p:fieldset>
                        </p:tab>
                    </p:tabView>
                </p:panel>
                <br />
                <p:panel header="Weekly Demand Management">
                    <h:panelGrid columns="2">
                        <p:fieldset legend ="Production Plan base on MPS" style="text-align:center">
                            <h:panelGrid columns="2">
                                <h:outputText value="Total Production Plan for #{masterProductionScheduleWeeklyEntityController.selected.monthOfProduction}:"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.plannedQuantity}"/>
                                <h:outputText value="Classic Nutri Bar (Nuts):"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.classicNuts}"/>
                                <h:outputText value="Classic Nutri Bar (Fruits):"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.classicFruits}"/>
                                <h:outputText value="Premium Power (Chocolate Nuts):"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.premiumNuts}"/>
                                <h:outputText value="Premium Power Bar (Redcurrant Fruits):"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.premiumFruits}"/>
                            </h:panelGrid>
                        </p:fieldset>
                        <p:fieldset legend ="Production Plan base on Demand Management" style="text-align:center">
                            <h:panelGrid columns="2">
                                <h:outputText value="Total Production Plan for #{masterProductionScheduleDailyEntityController.selected.mpsMonthly.monthOfProduction}:"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculateAllQuantity()}" style="color:red" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.plannedQuantity,masterProductionScheduleWeeklyEntityController.calculateAllQuantity())==false}"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculateAllQuantity()}" style="color:green" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.plannedQuantity,masterProductionScheduleWeeklyEntityController.calculateAllQuantity())==true}"/>
                                <h:outputText value="Classic Nutri Bar (Nuts):"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculateClassicNuts()}" style="color:red" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.classicNuts,masterProductionScheduleWeeklyEntityController.calculateClassicNuts())==false}"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculateClassicNuts()}" style="color:green" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.classicNuts,masterProductionScheduleWeeklyEntityController.calculateClassicNuts())==true}"/>
                                <h:outputText value="Classic Nutri Bar (Fruits):"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculateClassicFruits()}" style="color:red" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.classicFruits,masterProductionScheduleWeeklyEntityController.calculateClassicFruits())==false}"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculateClassicFruits()}" style="color:green" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.classicFruits,masterProductionScheduleWeeklyEntityController.calculateClassicFruits())==true}"/>
                                <h:outputText value="Premium Power (Chocolate Nuts):"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculatePremiumNuts()}" style="color:red" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.premiumNuts,masterProductionScheduleWeeklyEntityController.calculatePremiumNuts())==false}"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculatePremiumNuts()}" style="color:green" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.premiumNuts,masterProductionScheduleWeeklyEntityController.calculatePremiumNuts())==true}"/>
                                <h:outputText value="Premium Power Bar (Redcurrant Fruits):"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculatePremiumFruits()}" style="color:red" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.premiumFruits,masterProductionScheduleWeeklyEntityController.calculatePremiumFruits())==false}"/>
                                <h:outputText value="#{masterProductionScheduleWeeklyEntityController.calculatePremiumFruits()}" style="color:green" rendered="#{masterProductionScheduleDailyEntityController.checkEqual(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly.premiumFruits,masterProductionScheduleWeeklyEntityController.calculatePremiumFruits())==true}"/>
                            </h:panelGrid>
                        </p:fieldset>
                    </h:panelGrid>
                    <br />
                    <p:fieldset legend="Weekly Production Breakdown for #{masterProductionScheduleWeeklyEntityController.selected.monthOfProduction}">
                        <p:dataTable value="#{masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly)}" 
                                     var="week" style="table-layout:fixed">

                            <p:columnGroup type="header">  
                                <p:row rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==false}"> 
                                    <p:column rowspan="2" headerText=" " />
                                    <p:column rowspan="2" headerText="Week" />
                                    <p:column rowspan="2" headerText="Working Days in Week"/>
                                    <p:column colspan="2" headerText="Classic Nutri Bar" /> 
                                    <p:column colspan="2" headerText="Premium Power Bar" />
                                </p:row>  
                                <p:row rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==true}"> 
                                    <p:column rowspan="2" headerText=" " />
                                    <p:column rowspan="2" headerText="Week" />
                                    <p:column rowspan="2" colspan="2" headerText="Working Days in Week"/>
                                    <p:column colspan="4" headerText="Classic Nutri Bar" /> 
                                    <p:column colspan="4" headerText="Premium Power Bar" />
                                </p:row> 

                                <p:row rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==false}">  
                                    <p:column headerText="Nuts" />  
                                    <p:column headerText="Fruits" />  
                                    <p:column headerText="Chocolate Nuts" />  
                                    <p:column headerText="Redcurrant Fruits" />  
                                </p:row> 
                                <p:row rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==true}"> 
                                    <p:column colspan="2" headerText="Nuts" />  
                                    <p:column colspan="2" headerText="Fruits" />  
                                    <p:column colspan="2" headerText="Chocolate Nuts" />  
                                    <p:column colspan="2" headerText="Redcurrant Fruits" />  
                                </p:row>
                            </p:columnGroup>

                            <p:column style="width:16px">  
                                    <p:rowToggler />  
                            </p:column>
                            <p:column>
                                <h:outputText value="#{week.weekOfProduction}"/>
                            </p:column>
                            <p:column>
                                <h:outputText value="#{week.weeklyWorkingDays1}"/>
                            </p:column>
                            <p:column rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==true}">
                                <h:outputText value="#{week.weeklyWorkingDays2}"/>
                            </p:column>
                            <p:column>
                                <h:outputText value="#{week.classicNuts}"/>
                            </p:column>
                            <p:column rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==true}">
                                <h:outputText value="#{week.classicNuts2}"/>
                            </p:column>
                            <p:column>
                                <h:outputText value="#{week.classicFruits}"/>
                            </p:column>
                            <p:column rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==true}">
                                <h:outputText value="#{week.classicFruits2}"/>
                            </p:column>
                            <p:column>
                                <h:outputText value="#{week.premiumNuts}"/>
                            </p:column>
                            <p:column rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==true}">
                                <h:outputText value="#{week.premiumNuts2}"/>
                            </p:column>
                            <p:column>
                                <h:outputText value="#{week.premiumFruits}"/>
                            </p:column>
                            <p:column rendered="#{masterProductionScheduleWeeklyEntityController.checkForWeek5(masterProductionScheduleWeeklyEntityController.weeklyMPSForMonth(masterProductionScheduleWeeklyEntityController.oneDay.mpsMonthly))==true}">
                                <h:outputText value="#{week.premiumFruits2}"/>
                            </p:column>
                            
                            <p:rowExpansion>
                                <p:dataTable value="#{week.mpsDaily}" 
                                         var="item" style="table-layout:fixed"
                                         rows="5" >

                                <p:columnGroup type="header"> 
                                    <p:row> 
                                        <p:column rowspan="2" headerText="Date" />
                                        <p:column colspan="2" headerText="Classic Nutri Bar" /> 
                                        <p:column colspan="2" headerText="Premium Power Bar" />
                                    </p:row>   

                                    <p:row>  
                                        <p:column headerText="Nuts" />  
                                        <p:column headerText="Fruits" />  
                                        <p:column headerText="Chocolate Nuts" />  
                                        <p:column headerText="Redcurrant Fruits" />  
                                    </p:row>  
                                </p:columnGroup>

                                <p:column>
                                    <h:outputText value="#{item.dayofProduction}"/>
                                </p:column>
                                <p:column>
                                    <h:outputText value="#{item.classicNuts}"/>
                                </p:column>
                                <p:column>
                                    <h:outputText value="#{item.classicFruits}"/>
                                </p:column>
                                <p:column>
                                    <h:outputText value="#{item.premiumNuts}"/>
                                </p:column>
                                <p:column>
                                    <h:outputText value="#{item.premiumFruits}"/>
                                </p:column>

                            </p:dataTable>
                            </p:rowExpansion>

                        </p:dataTable>
                    </p:fieldset>
                </p:panel>
                <br />
                <p:panel header="Meta Information" toggleable="true" toggleSpeed="500" collapsed="true">
                    <h:panelGrid columns="4">
                        <h:outputText value="Created By:"/>
                        <h:outputText value="#{masterProductionScheduleWeeklyEntityController.selected.createdBy.firstName} #{masterProductionScheduleWeeklyEntityController.selected.createdBy.lastName}"/>
                        <h:outputText value="Last Modified By:"/>
                        <h:outputText value="#{masterProductionScheduleWeeklyEntityController.selected.lastModifiedBy.firstName} #{masterProductionScheduleWeeklyEntityController.selected.lastModifiedBy.lastName}"/>
                        <h:outputText value="Created At:"/>
                        <h:outputText value="#{masterProductionScheduleWeeklyEntityController.selected.createdAt}">
                            <f:convertDateTime pattern="dd/MM/yyyy" />
                        </h:outputText>
                        <h:outputText value="Last Modified At:"/>
                        <h:outputText value="#{masterProductionScheduleWeeklyEntityController.selected.lastModifiedAt}">
                            <f:convertDateTime pattern="dd/MM/yyyy" />
                        </h:outputText>
                    </h:panelGrid>
                </p:panel>
                <p:commandButton ajax="false" value="Show All Weekly Demand Management" action="#{masterProductionScheduleWeeklyEntityController.prepareList}"/>
            </h:form>
        </ui:define>
    </ui:composition>

</html>